package com.newjiabo.applet.modules.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.CharSequenceUtil;
import com.newjiabo.applet.modules.entity.enums.SourceEnum;
import com.newjiabo.applet.modules.service.ConnectService;
import com.newjiabo.framework.web.service.TokenService;
import com.newjiabo.ski.domain.SkiConnect;
import com.newjiabo.ski.mapper.SkiConnectMapper;
import com.newjiabo.ski.mapper.SkiMemberMapper;
import com.newjiabo.applet.modules.entity.dto.ConnectAuthUser;
import com.newjiabo.applet.modules.service.MemberService;
import com.newjiabo.common.core.domain.entity.SkiMember;
import com.newjiabo.common.core.enums.ResultCode;
import com.newjiabo.common.exception.ServiceException;
import com.newjiabo.common.utils.DateUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.uuid.UuidUtils;
import com.newjiabo.ski.service.ISkiMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * {类内容}
 *
 * @author CaoMengHui.
 * @version 1.0
 * @since 2023/9/15 11:16
 */
@Service
public class MemberServiceImpl implements MemberService {
    private static final Logger log = LoggerFactory.getLogger(MemberServiceImpl.class);
    @Resource
    private SkiMemberMapper skiMemberMapper;
    @Resource
    private TokenService tokenService;
    @Resource
    private ConnectService connectService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public SkiMember autoRegister(ConnectAuthUser authUser) {
        if (CharSequenceUtil.isEmpty(authUser.getNickname())) {
            authUser.setNickname("临时昵称");
        }
        if (CharSequenceUtil.isEmpty(authUser.getAvatar())) {
            authUser.setAvatar("https://i.loli.net/2020/11/19/LyN6JF7zZRskdIe.png");
        }
        try {
            SkiMember skiMember = new SkiMember(authUser.getUsername(), UuidUtils.getUUID(), authUser.getAvatar(), authUser.getNickname(),
                    authUser.getGender() != null ? Convert.toInt(authUser.getGender().getCode()) : 0, authUser.getPhone(),false);
            skiMember.setPassword(new BCryptPasswordEncoder().encode(DEFAULT_PASSWORD));
            // 发送会员注册信息
            skiMember.setId(SnowFlake.getIdStr());
            skiMemberMapper.insertSkiMember(skiMember);
            SourceEnum sourceEnum = SourceEnum.getSourceEnum(authUser.getSource(), authUser.getType());
            connectService.insertSkiConnect(new SkiConnect(skiMember.getId(), authUser.getUuid(), sourceEnum.name()));
            return skiMember;
        } catch (ServiceException e) {
            log.error("自动注册服务抛出异常：", e);
            throw e;
        } catch (Exception e) {
            log.error("自动注册异常：", e);
            throw new ServiceException(ResultCode.USER_AUTO_REGISTER_ERROR);
        }
    }

    @Override
    public SkiMember findByMobile(String phone) {
        return skiMemberMapper.findByMobile(phone);
    }


    @Override
    public String refreshToken(String refreshToken) {
        return tokenService.refreshToken(refreshToken);
    }
}
